'use strict';

import { merchant_commentModel } from "../model/merchant_comment";
import { ServiceBase, Exceptions } from '../kalend';
import config from "../middleware/config";
import { customer_orderService } from "./customer_order";
var moment = require('moment');

class Merchant_commentService extends ServiceBase {
    constructor() {
        super(merchant_commentModel)
    }
    async query(params: any) {
        const offset = params.offset || 1;
        const limit = params.limit || 15;
        const mid = params.mid;
        let data = await super.queryPagination(offset, limit, {
            filter: { merchantid: mid },
            select: 'start content imgs customerid updateTime merchantid',
            populateOptions: [{
                path: 'customerid',
                select: 'nickname headimg'
            }, {
                path: 'merchantid',
                select: 'logoimage'
            }]
        });
        return data.rows.map((item) => {
            if (item.updateTime) {
                item.updateTime = moment(item.updateTime).format('YYYY-MM-DD HH:mm');
            }
            return item;
        });
        //return data;
    }

    async save(pars: any) {
        if (!pars.orderCode) {
            throw Exceptions.known("请传入订单号");
        }
        if (!pars.star && !pars.content) {
            throw Exceptions.known('请选择星级或输入评论');
        }
        let orderdata = await customer_orderService.querySingle({ code: pars.orderCode });
        let doc: any = {
            merchantid: orderdata.merchantid,
            customerid: orderdata.customerid,
            star: pars.star || 0,
            content: pars.content,
            lables: pars.lables,
            orderid: orderdata._id
        };
        this.create(doc);
        return '评论成功';
    }
    async queryItems(filter: any) {
        return this.queryAll({ filter: filter });
    }
}

export const merchant_commentService = new Merchant_commentService();